// pages/usercenter/pladge/pladge.js
import {
  users
} from '../../../../tools/users'
import {
  ui
} from '../../../../tools/ui'
import {
  pay
} from '../../../../tools/pay'
import {
  checkTool
} from '../../../../tools/check'
import {
  msg
} from '../../../../tools/msg'
import {
  ds
} from '../../../../tools/data'
import {
  dsx
} from '../../../../tools/datax'
import {
  dateTool
} from '../../../../tools/date';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    refoundUsers: null,
  },
  onLoad(options) {
    this.init()
  },
  init() {
    ui.loading()
    this.loadRefoundUsers()
  },
  async loadRefoundUsers() {
    dsx.getAll('users', {
      "barrelPay.list": dsx.cmd.elemMatch({
        payStatus: "refound"
      })
    }).then(async res => {
      for (const user of res) {
        // get addr
        const addrRes = await ds.coll('delivery_addr').where({
          _openid: ds.cmd.eq(user._openid)
        }).get()
        user.addr = addrRes.data[0]

        // get update time
        let updateTime = 0
        for (const item of user.barrelPay.list) {
          if (item.payStatus == 'refound' && item.timeCreate > updateTime) {
            updateTime = item.timeCreate
          }
        }
        user.barrelPay.updateTime = updateTime
      }
      this.setData({
        refoundUsers: res
      })
      ui.loading(false)
    })
  },

  callMobile(e) {
    const mobile = ui.eventData(e, "mobile")
    wx.makePhoneCall({
      phoneNumber: mobile,
    })
  },
  nickNameTap(e) {
    const uid = ui.eventData(e, 'uid')
    wx.navigateTo({
      url: `/pages/usercenter/pladge/pladge?action=manage&staffId=${uid}`,
    })
  },



  onPullDownRefresh() {
    this.init(this.data.action)
  }
})